*this file takes residualised log earnings and prepares them for computation of moments
*these are the moments of the individual covariance structure  
*it calls the file covaxn.do which computes moments by cohort and member type
*it calls the file accinit.do , which stacks moments over cohorts and member type


clear all 
set more off
cap log close

set mem 3000m
set maxvar 30000
log using merxn.log,replace


global b=0
while $b<=2 {
	global i=${min_${b}}
	while $i<=${max_${b}} {
		di "$i $b"
		use  res_${b}_${i} ,clear
		sort year
		global from=year[1]
		global to=year[_N]
		local j=$from
		while `j'<=$to {
			preserve
			keep if year==`j'
			sort pnr
			rename res res`j'
			rename ageA ageA`j'
			rename ageC ageC`j'
			save  err_${b}_`j',replace
			restore
			local j=`j'+1
		}
		do  covaxn  /*computes second moments, by birth cohort*/
		global i=$i+3
	}
	local co=${min_${b}}
	while `co'<=${max_${b}} {
		capture erase  res_${b}_`co'.dta
		local co=`co'+3
	}
	local i=1980
	while `i'<=2014{
		capture erase  err_${b}_`i'.dta
		local i=`i'+1
	}
	do  accinit all ${min_${b}} ${max_${b}}
	global b=$b+1
}
	
	
	
	















